﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

namespace Hello_World
{
    public partial class Xiaonei_AddArticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public CookieContainer login(string username, string password)
        {
            CookieContainer cookies = new CookieContainer();
            HttpWebRequest logreq = (HttpWebRequest)WebRequest.Create("http://login.xiaonei.com/Login.do");
            logreq.CookieContainer = cookies;
            byte[] postdata = System.Text.Encoding.UTF8.GetBytes("email=" + username + "&password=" + password);
            logreq.Method = "POST";
            logreq.ContentLength = postdata.Length;
            logreq.ContentType = "application/x-www-form-urlencoded";
            logreq.GetRequestStream().Write(postdata, 0, postdata.Length);
            logreq.GetRequestStream().Close();
            logreq.GetResponse();
            return cookies;
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            string user = txtusername.Text;
            string pwd = txtuserpwd.Text;

            string blogtitle = txttitle.Text;
            string blogbody = txtbody.Text;

            CookieContainer cookies = login(user, pwd);

            HttpWebRequest sendreq = (HttpWebRequest)WebRequest.Create("http://blog.xiaonei.com/NewEntry.do");
            byte[] message = System.Text.Encoding.UTF8.GetBytes("title=" + blogtitle + "&body=" + blogbody + "&categoryid=0&blogControl=99&passwordProtedted=0");
            sendreq.ContentType = "application/x-www-form-urlencoded";
            sendreq.Method = "POST";
            sendreq.ContentLength = message.Length;
            sendreq.CookieContainer = cookies;
            sendreq.GetRequestStream().Write(message, 0, message.Length);
            sendreq.GetRequestStream().Close();
            sendreq.GetResponse();
        }
    }
}